package com.guigu.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.base.entity.TruckDriver;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.base.mapper.TruckDriverMapper;
import com.guigu.base.service.TruckDriverService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 司机表 服务实现类
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@Service
public class TruckDriverServiceImpl extends ServiceImpl<TruckDriverMapper, TruckDriver> implements TruckDriverService {

    /**
     * 添加司机
     *
     * @param pdTruckDriver 司机信息
     * @return 司机信息
     */
    @Override
    public TruckDriver saveTruckDriver(TruckDriver pdTruckDriver) {
        TruckDriver driver = baseMapper.selectOne(new LambdaQueryWrapper<TruckDriver>().eq(TruckDriver::getUserId, pdTruckDriver.getUserId()));
        if (driver == null) {

        } else {
            pdTruckDriver.setId(driver.getId());
        }
        saveOrUpdate(pdTruckDriver);
        return pdTruckDriver;
    }

    /**
     * 获取司机基本信息列表
     *
     * @param userIds 司机id列表
     * @param fleetId
     * @return 司机基本信息列表
     */
    @Override
    public List<TruckDriver> findAll(List<String> userIds, String fleetId) {
        boolean hasUserIds = userIds != null && userIds.size() > 0;
        boolean hasFleetId = StringUtils.isNotEmpty(fleetId);
        if (!hasUserIds && !hasFleetId) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<TruckDriver> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (hasUserIds) {
            lambdaQueryWrapper.in(TruckDriver::getUserId, userIds);
        }
        if (hasFleetId) {
            lambdaQueryWrapper.eq(TruckDriver::getFleetId, fleetId);
        }
        lambdaQueryWrapper.orderBy(true, false, TruckDriver::getId);
        return baseMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 获取司机基本信息
     *
     * @param userId 司机id
     * @return 司机基本信息
     */
    @Override
    public TruckDriver findOne(String userId) {
        LambdaQueryWrapper<TruckDriver> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(userId)) {
            lambdaQueryWrapper.eq(TruckDriver::getUserId, userId);
        }
        return getOne(lambdaQueryWrapper);
    }

    /**
     * 统计司机数量
     *
     * @param fleetId 车队id
     * @return 司机数量
     */
    @Override
    public Integer count(String fleetId) {
        LambdaQueryWrapper<TruckDriver> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(fleetId)) {
            lambdaQueryWrapper.eq(TruckDriver::getFleetId, fleetId);
        }
        return count(lambdaQueryWrapper);
    }

    /**
     * 获取司机分页数据
     *
     * @param page     页码
     * @param pageSize 页尺寸
     * @param fleetId  车队id
     * @return 司机分页数据
     */
    @Override
    public IPage<TruckDriver> findByPage(Integer page, Integer pageSize, String fleetId) {
        Page<TruckDriver> iPage = new Page(page, pageSize);
        LambdaQueryWrapper<TruckDriver> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(fleetId)) {
            lambdaQueryWrapper.eq(TruckDriver::getFleetId, fleetId);
        }
        lambdaQueryWrapper.orderBy(true, false, TruckDriver::getId);
        return baseMapper.selectPage(iPage, lambdaQueryWrapper);
    }
}
